import processing.core.*; 
import processing.xml.*; 

import java.io.File; 
import java.util.Scanner; 

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class networkdraw extends PApplet {




int ws = 500; 
int maxnumsegments = 200;
float[] x1 = new float[maxnumsegments];
float[] x2 = new float[maxnumsegments];
float[] y1 = new float[maxnumsegments];
float[] y2 = new float[maxnumsegments];
int[] vertx = new int[0];
int[] verty = new int[0];
int[] solution = new int[0];
int numsegs = 0;
boolean stateNewLine = true;
String mydir = "/home/mdm32/sketchbook/networkdraw/GSS/";
String outfile = "gss2.txt";
String currentimage = "gss2.tif";
String fileName = null;

public void setup(){
  System.out.print("Enter the name of the GSS we will save to: ");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  try {
    fileName = br.readLine();
  } catch (IOException e) {
      System.out.println("Error reading file");
      System.exit(1);
  }
  outfile = fileName + ".txt";
  currentimage = fileName  + ".tif";
  println("segment #: x1, y1, x2, y2");
  size(ws,ws);
  fill(255,0,0);
}

public void draw(){
  background(0);
  stroke(255);
  for(int i=0; i < numsegs; i++){
    line(x1[i],y1[i],x2[i],y2[i]);
  }
  stroke(225,0,0);
  fill(221,160,221);
  for(int i=0; i < numsegs; i++){
    ellipse(x1[i], y1[i], 10, 10);
    ellipse(x2[i], y2[i], 10, 10);
  }
  
  for(int i = 0; i < vertx.length; i++){
      ellipse(vertx[i], verty[i], 10, 10);
  }
  
  if(solution.length > 0){
    for(int i = 0; i < vertx.length; i++){
      fill(0,255,0);
      if(solution[i]==1){
        ellipse(vertx[i],verty[i],10,10);
      }
    }
  }
  
  if(stateNewLine==false){
    line(x1[numsegs],y1[numsegs],mouseX, mouseY);
  }
  
  
}

public void mouseReleased() {
    if(stateNewLine==true){
      x1[numsegs]=mouseX;
      y1[numsegs]=mouseY;
      stateNewLine=false;
    }
    else{
      x2[numsegs]=mouseX;
      y2[numsegs]=mouseY;
      println((numsegs+1) + ": " + x1[numsegs] + ", " + y1[numsegs] + ", " + x2[numsegs] + ", " + y2[numsegs]);
      stateNewLine=true;
      numsegs++;
    }
}
  
public void keyPressed() {
  if (key == 's') {
    //Writing to a file:
    try{
      File fdelete = new File((mydir + outfile));
      fdelete.delete();
      BufferedWriter out = new BufferedWriter(new FileWriter((mydir + outfile),true));
      for(int i=0; i < numsegs; i++){
        out.write(i + " " + x1[i] + " " + y1[i] + " " + x2[i] + " " + y2[i] + "\n");
      }
      save(mydir + currentimage);
      out.close();
    } catch(IOException e) {
      e.printStackTrace();
      }
  }
  if(key == 'd'){
    int a = 0; int b = 0;
    stroke(225,0,0);
    fill(225,160,221);
    String points[] = loadStrings(mydir + fileName + "intersect1.txt");
    vertx = new int[points.length/2];
    verty = new int[points.length/2];
    for(int i = 0; i < points.length; i+=2){
      vertx[i/2] = Integer.parseInt(points[i]);
      verty[i/2] = Integer.parseInt(points[i+1]);
      ellipse(a, b, 10, 10);
    }
    save(mydir + currentimage);

  }
  if(key == 'f'){
    String solu[] = loadStrings(mydir + fileName + "solution.txt");
    solution = new int[solu.length];
    for(int i = 0; i < solu.length; i++){
      solution[i] = Integer.parseInt(solu[i]);
    }  
    fill(0,255,0);
    for(int i = 0; i < vertx.length; i++){
      if(solution[i]==1){
        ellipse(vertx[i],verty[i],10,10);
      }
    }
  }
}

  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#DFDFDF", "networkdraw" });
  }
}
